var index = parent.layer.getFrameIndex(window.name);
var output_id = parent.$("#output_id").val();
var dSuffix = parent.$("#suffix").val();
var type = parent.$("#type").val();
var isDefault = false;
window.onload = function () {
    if(output_id.split("~")[0]==0){
        isDefault = true;
    }
    $("#type").val(type);
    if (type == "file") {
        if(isDefault){
            $("#add").html('<h3>当前选择:<br><span id="url" style="display: block;margin-bottom: 3px;margin-top: 3px;"></span><input type="text" placeholder="无需输入文件名" class="form-control form-group" id="document_name" style="display: inline-block; width: 49%" disabled="disabled">.<input type="text" placeholder="输出文件后缀" class="form-control form-group" id="document_suffix" style="display: inline-block; width: 49%"></h3>');
        } else{
            $("#add").html('<h3>当前选择:<br><span id="url" style="display: block;margin-bottom: 3px;margin-top: 3px;"></span><input type="text" placeholder="输出文件名" class="form-control form-group" id="document_name" style="display: inline-block; width: 49%">.<input type="text" placeholder="输出文件后缀" class="form-control form-group" id="document_suffix" style="display: inline-block; width: 49%"></h3>');
        }
    } else {
        $("#add").html('<h3>当前选择:<br><span id="url" style="display: block;"></span><input type="text" placeholder="输出文件夹名" class="form-control form-group" id="document_name"></h3>');
    }
    $("#document_suffix").val(dSuffix);
    if (output_id.split("~")[1]==0&&$("#isDefaultPath", window.parent.document).val()==1) {
        getDocument($("#defaultPathTemp", window.parent.document).val());
    } else {
        getDocument("");
    }
    $('#saveDocument').click(function () {
        var type_temp = $("#type").val();
        var temp_url = document.getElementById("url").innerHTML;
        var url = temp_url.substring(1, temp_url.length);
        var name = $("#document_name").val();
        if (type_temp == "file") {
            var suffix = $("#document_suffix").val();
            if(isDefault){
                if (name != "") {
                    layer.alert("请勿输入输出文件名！");
                    return false;
                } else if (suffix == "") {
                    layer.alert("请输入输出文件后缀！");
                    return false;
                } else {
                    var finalName = "." + suffix;
                    var a, b, c;
                    a = output_id.split("~")[0];
                    b = output_id.split("~")[1];
                    c = output_id.split("~")[2];
                    if (a == "0") {
                        if (b==0&&c==0){
                            $("#defaultPathTemp", window.parent.document).val(temp_url.substring(0, temp_url.length-1));
                        }
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputValue").val(finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").val(url + finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + finalName);
                    } else {
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputValue").val(finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").val(url + finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + finalName);
                    }
                    parent.layer.close(index);
                }
            } else {
                if (name == "") {
                    layer.alert("请输入输出文件名！");
                    return false;
                } else if (suffix == "") {
                    layer.alert("请输入输出文件后缀！");
                    return false;
                } else {
                    var finalName = name + "." + suffix;
                    var a, b, c;
                    a = output_id.split("~")[0];
                    b = output_id.split("~")[1];
                    c = output_id.split("~")[2];
                    if (a == "0") {
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputValue").val(finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").val(url + finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + finalName);
                    } else {
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputValue").val(finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").val(url + finalName);
                        $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + finalName);
                    }
                    parent.layer.close(index);
                }
            }
        } else {
            if (name == "") {
                var url_temp = url.split("/");
                url = "";
                if(url_temp.length > 2){
                    for(var i=0; i<url_temp.length-2; i++){
                        url += url_temp[i] + "/";
                    }
                    name = url_temp[url_temp.length-2];
                } else {
                    name = "";
                    url = "";
                }
                var a, b, c;
                a = output_id.split("~")[0];
                b = output_id.split("~")[1];
                c = output_id.split("~")[2];
                if (a == "0") {
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputValue").val(name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").val(url + name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + name);
                } else {
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputValue").val(name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").val(url + name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + name);
                }
                parent.layer.close(index);
            } else {
                var a, b, c;
                a = output_id.split("~")[0];
                b = output_id.split("~")[1];
                c = output_id.split("~")[2];
                if (a == "0") {
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".outputValue").val(name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").val(url + name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".defaultBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + name);
                } else {
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputAddress").val(url);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".outputValue").val(name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").val(url + name);
                    $("#output_form", window.parent.document).children(".table_div").children("#outputTabContent").children("#task_" + a).children(".oneBlock").eq(b).children(".input").eq(c).children(".output").attr("title", url + name);
                }
                parent.layer.close(index);
            }
        }
    });
    $('#closeIframe').click(function () {
        parent.layer.close(index);
    });
}
returnPrevious = function () {
    var url = document.getElementById("url");
    var urlHtml = url.innerHTML;
    var urlHtml_one = urlHtml.split("/");
    urlHtml = "";
    if (urlHtml_one.length > 2) {
        for (var i = 0; i < urlHtml_one.length - 2; i++) {
            urlHtml += urlHtml_one[i] + "/";
        }
    } else {
        urlHtml += "/";
    }
    url.innerHTML = urlHtml;
    urlHtml = urlHtml.substring(1, urlHtml.length);
    if (urlHtml == "/")
        urlHtml = "";
    $.ajax({
        url: constUrl + "GetFilePath",
        type: 'post',
        dataType: 'json',
        data: {
            "url": urlHtml
        },
        success: function (responseText) {
            if (responseText.result) {
                var jsonData = responseText;
                creatHtml(jsonData.message);
            } else {
                layer.alert("获取数据失败，请重试！");
            }
        },
        complete: function () {
        },
        error: function () {
            layer.alert("获取数据失败！")
        }
    });
}
getDocument = function (DocumentName) {
    var url = $("#url");
    url.html(url.html() + DocumentName + "/");
    var urlHtml = url.html();
    //删除第一个"/"
    urlHtml = urlHtml.substring(1, urlHtml.length);
    $.ajax({
        url: constUrl + "GetFilePath",
        type: 'post',
        dataType: 'json',
        data: {
            "url": urlHtml
        },
        success: function (responseText) {
            if (responseText.result) {
                var jsonData = responseText;
                creatHtml(jsonData.message);
            } else {
                layer.alert("获取数据失败，请重试！");
            }
        },
        complete: function () {
        },
        error: function () {
            layer.alert("获取数据失败！")
        }
    });
}
creatHtml = function (jsonData) {
    var document_form = document.getElementById("document_form");
    document_form.innerHTML = "";
    document_form.innerHTML += '<a href="##" onclick="returnPrevious()">../</a>';
    for (var i = 0; i < jsonData.length; i++) {
        if (jsonData[i].type == "DIRECTORY") {
            document_form.innerHTML += '<a href="##" onclick="getDocument(\'' + jsonData[i].pathSuffix + '\')"><i class="am-icon-folder-open"/>&nbsp;' + jsonData[i].pathSuffix + '</a>';
        } else {
            document_form.innerHTML += '<a href="##"><i class="glyphicon glyphicon-file"/>&nbsp;' + jsonData[i].pathSuffix + '</a>';
        }
    }
    var document_name = document.getElementById("document_name");
    document_name.innerHTML = "";
}